import { defineStore } from "pinia";
import _hook from "@/hooks/use/index.js";
import {
  login,
  getUserInfo,
  getUserList,
  addUser,
  updateUser,
  deleteUser,
} from "@/api/user.js";

export const useUser = defineStore("useUser", {
  state: () => {
    return {
      userInfo: {},
      token: _hook.useSessionStorage.get("token") || "",
      userList: [],
      totalUsers: 0,
    };
  },
  getters: {
    // 获取用户角色
    userRoles: (state) => state.userInfo.roles || [],
    // 获取用户权限
    userPermissions: (state) => state.userInfo.permissions || [],
    // 检查是否有特定权限
    hasPermission: (state) => (permission) => {
      return (
        state.userInfo.permissions?.includes("*") ||
        state.userInfo.permissions?.includes(permission)
      );
    },
    // 检查是否有特定角色
    hasRole: (state) => (role) => {
      return state.userInfo.roles?.includes(role);
    },
  },
  actions: {
    // 设置用户信息
    async setUserInfo() {
      if (_hook.useSessionStorage.get("userInfo")) {
        this.userInfo = _hook.useSessionStorage.get("userInfo");
      } else {
        this.userInfo = await this.getUserInfo();
      }
    },
    /**
     * @description: 用户登录
     * @param {*} param: 登录的参数
     */
    userlogin(param) {
      return login(param).then((res) => {
        this.userInfo = res.userInfo;
        this.token = res.userInfo.token;
        _hook.useSessionStorage.set("token", this.token);
        _hook.useSessionStorage.set("userInfo", this.userInfo);
        return this.userInfo;
      });
    },
    /**
     * @description: 获取用户信息
     */
    getUserInfo() {
      return getUserInfo().then((res) => {
        return res.userInfo;
      });
    },
    /**
     * @description: 获取用户列表
     */
    async getUserList(params = {}) {
      try {
        const res = await getUserList(params);
        this.userList = res.list;
        this.totalUsers = res.total;
        return res;
      } catch (error) {
        console.error("获取用户列表失败:", error);
        throw error;
      }
    },
    /**
     * @description: 添加用户
     */
    async addUser(userData) {
      try {
        const res = await addUser(userData);
        // 重新获取用户列表
        await this.getUserList();
        return res;
      } catch (error) {
        console.error("添加用户失败:", error);
        throw error;
      }
    },
    /**
     * @description: 更新用户
     */
    async updateUser(id, userData) {
      try {
        const res = await updateUser(id, userData);
        // 重新获取用户列表
        await this.getUserList();
        return res;
      } catch (error) {
        console.error("更新用户失败:", error);
        throw error;
      }
    },
    /**
     * @description: 删除用户
     */
    async deleteUser(id) {
      try {
        const res = await deleteUser(id);
        // 重新获取用户列表
        await this.getUserList();
        return res;
      } catch (error) {
        console.error("删除用户失败:", error);
        throw error;
      }
    },
    /**
     * @description: 用户登出
     */
    logout() {
      this.userInfo = {};
      this.token = "";
      this.userList = [];
      this.totalUsers = 0;
      _hook.useSessionStorage.clear();
    },
  },
});
